<html>
<head><meta charset="utf-8"><title>Can you implement a mutable iterator without unsafe? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20you.20implement.20a.20mutable.20iterator.20without.20unsafe.3F.html">Can you implement a mutable iterator without unsafe?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="248300159"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20you%20implement%20a%20mutable%20iterator%20without%20unsafe%3F/near/248300159" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20you.20implement.20a.20mutable.20iterator.20without.20unsafe.3F.html#248300159">(Aug 04 2021 at 00:57)</a>:</h4>
<p>TIL that the current Iterator trait makes it impossible to implement mutable iterators over collections without unsafe (I think): <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=acbf48c73582d44fc47c7a27711b81f3">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=acbf48c73582d44fc47c7a27711b81f3</a> (I think?)</p>
<p>is there an interest in formulating some backwards compatible way to upgrade Iterator into more generic interface once GAT's land (<a href="https://blog.rust-lang.org/2021/08/03/GATs-stabilization-push.html">https://blog.rust-lang.org/2021/08/03/GATs-stabilization-push.html</a>)?</p>



<a name="248300843"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20you%20implement%20a%20mutable%20iterator%20without%20unsafe%3F/near/248300843" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20you.20implement.20a.20mutable.20iterator.20without.20unsafe.3F.html#248300843">(Aug 04 2021 at 01:07)</a>:</h4>
<p>the impossible thing in <code>Iterator</code> is mutable <em>overlapping</em> items, because they can't borrow from the iterator itself</p>



<a name="248300892"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20you%20implement%20a%20mutable%20iterator%20without%20unsafe%3F/near/248300892" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20you.20implement.20a.20mutable.20iterator.20without.20unsafe.3F.html#248300892">(Aug 04 2021 at 01:08)</a>:</h4>
<p>you could write that <code>Gus&lt;T&gt;</code> returning distinct <code>&amp;mut T</code> though</p>



<a name="248300940"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20you%20implement%20a%20mutable%20iterator%20without%20unsafe%3F/near/248300940" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20you.20implement.20a.20mutable.20iterator.20without.20unsafe.3F.html#248300940">(Aug 04 2021 at 01:09)</a>:</h4>
<p>like, you can <code>slice.split_first_mut</code> each time, returning one reference while keeping the remainder</p>



<a name="248301034"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20you%20implement%20a%20mutable%20iterator%20without%20unsafe%3F/near/248301034" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20you.20implement.20a.20mutable.20iterator.20without.20unsafe.3F.html#248301034">(Aug 04 2021 at 01:11)</a>:</h4>
<p><code>slice::MutIter</code> without <code>unsafe</code>: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=b083b1368305a57b982d5ba27e915f50">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=b083b1368305a57b982d5ba27e915f50</a></p>



<a name="248301511"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20you%20implement%20a%20mutable%20iterator%20without%20unsafe%3F/near/248301511" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20you.20implement.20a.20mutable.20iterator.20without.20unsafe.3F.html#248301511">(Aug 04 2021 at 01:20)</a>:</h4>
<p><span class="user-mention silent" data-user-id="138448">cuviper</span> <a href="#narrow/stream/122651-general/topic/Can.20you.20implement.20a.20mutable.20iterator.20without.20unsafe.3F/near/248300940">said</a>:</p>
<blockquote>
<p>like, you can <code>slice.split_first_mut</code> each time, returning one reference while keeping the remainder</p>
</blockquote>
<p>ah yes, that makes sense</p>
<p>I think my intuition though, is that in general its hard/impossible to prove to the compiler that mutable references don't overlap without <code>unsafe</code>, but that a LendingIterator impl, as suggested by the post would be easy to implement, as you are actually guaranteeing that the references dont overlap by the fact that the lifetimes of the references are bound the the <code>&amp;mut self</code> borrow</p>
<p>obviously this is less usable than say <code>IterMut</code> in the std lib: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=b083b1368305a57b982d5ba27e915f50">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=b083b1368305a57b982d5ba27e915f50</a>, but intuitively it makes more sense to me, and its kindof a testament to the design of std that I was using these mutable iterators without thinking about the fact that someone (not the compiler) is guaranteeing that the references dont overlap...to the point where I can use all the references returned by a IterMut at the same time and its just fine (<a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=da276abc28e777a9edb14317c589d36d">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=da276abc28e777a9edb14317c589d36d</a>)</p>
<p>That said, I think that <code>LendingIterator</code> may very well become fairly heavily used, not just for things like "iterators of overlapping mutable subslices", but also for easier to implement but slightly harder to use iterators</p>



<a name="248301542"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20you%20implement%20a%20mutable%20iterator%20without%20unsafe%3F/near/248301542" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20you.20implement.20a.20mutable.20iterator.20without.20unsafe.3F.html#248301542">(Aug 04 2021 at 01:21)</a>:</h4>
<p><span class="user-mention silent" data-user-id="229517">Jacob Lifshay</span> <a href="#narrow/stream/122651-general/topic/Can.20you.20implement.20a.20mutable.20iterator.20without.20unsafe.3F/near/248301034">said</a>:</p>
<blockquote>
<p><code>slice::MutIter</code> without <code>unsafe</code>: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=b083b1368305a57b982d5ba27e915f50">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=b083b1368305a57b982d5ba27e915f50</a></p>
</blockquote>
<p>oh wow! is the match arm doing the same thing as split_first_mut basically?</p>



<a name="248303113"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20you%20implement%20a%20mutable%20iterator%20without%20unsafe%3F/near/248303113" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20you.20implement.20a.20mutable.20iterator.20without.20unsafe.3F.html#248303113">(Aug 04 2021 at 01:55)</a>:</h4>
<p><span class="user-mention silent" data-user-id="257428">Gus Wynn</span> <a href="#narrow/stream/122651-general/topic/Can.20you.20implement.20a.20mutable.20iterator.20without.20unsafe.3F/near/248301542">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="229517">Jacob Lifshay</span> <a href="#narrow/stream/122651-general/topic/Can.20you.20implement.20a.20mutable.20iterator.20without.20unsafe.3F/near/248301034">said</a>:</p>
<blockquote>
<p><code>slice::MutIter</code> without <code>unsafe</code>: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=b083b1368305a57b982d5ba27e915f50">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=b083b1368305a57b982d5ba27e915f50</a></p>
</blockquote>
<p>oh wow! is the match arm doing the same thing as split_first_mut basically?</p>
</blockquote>
<p>yes. I could have used <code>split_first_mut</code>, but was trying to avoid all function calls other than the strictly necessary ones to avoid relying on any unsafe code</p>



<a name="248317730"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20you%20implement%20a%20mutable%20iterator%20without%20unsafe%3F/near/248317730" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matt1992 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20you.20implement.20a.20mutable.20iterator.20without.20unsafe.3F.html#248317730">(Aug 04 2021 at 07:16)</a>:</h4>
<blockquote>
<p>but was trying to avoid all function calls other than the strictly necessary ones to avoid relying on any unsafe code</p>
</blockquote>
<p><code>split_first_mut</code> is implemented in terms of <code>if let [first, tail @ ..]</code> <br>
<a href="https://github.com/rust-lang/rust/blob/effea9a2a0d501db5722d507690a1a66236933bf/library/core/src/slice/mod.rs#L203">https://github.com/rust-lang/rust/blob/effea9a2a0d501db5722d507690a1a66236933bf/library/core/src/slice/mod.rs#L203</a></p>
<p>It changed to using that in <a href="https://github.com/rust-lang/rust/commit/53be0ccbc913f05c81f1762fd60512e3f57ab5c7">https://github.com/rust-lang/rust/commit/53be0ccbc913f05c81f1762fd60512e3f57ab5c7</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>